package com.flower.wholesale.controller;


import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flower.wholesale.entity.ProductType;
import com.flower.wholesale.enu.StatausEnum;
import com.flower.wholesale.service.ProductTypeService;
import java.io.IOException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 * <p>
 * 花卉分类表 前端控制器
 * </p>
 *
 * @author 姚雁飞
 * @since 2020-01-15
 */
@Controller
@RequestMapping("/product-type")
public class ProductTypeController extends BaseController{
	
	private static final Logger logger = LoggerFactory.getLogger(ProductTypeController.class);
	
	@Autowired
	private ProductTypeService productTypeService;
	
	/**
	 * 
	 * @author: 姚雁飞
	 * @desc: 商品分类分页查询
	 * @date: 2020年4月20日 下午4:57:49
	 * @param request
	 * @param response
	 * @param name 分类名称
	 * @param pageNo  查询页码
	 * @param pageSize  每页显示的条数
	 * @throws IOException
	 */
	@PostMapping("/getPager")
	public void getPager(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "name", required = false, defaultValue = "") String name,
			@RequestParam(value = "status", required = false, defaultValue = "") String status,
			@RequestParam(value = "pageNo", required = true,defaultValue = "1") int pageNo,
			@RequestParam(value = "pageSize", required = true, defaultValue = "10") int pageSize
			) throws IOException {
		JSONObject result = new JSONObject();
		result.put("code", StatausEnum.SUCCESS.getCode());
		result.put("msg",  StatausEnum.SUCCESS.getMsg());
		QueryWrapper<ProductType> queryWrapper = new QueryWrapper<ProductType>();
		if(StringUtils.isNotEmpty(name)) {
			queryWrapper.like("name", name);
		}
		if(StringUtils.isNotEmpty(status)) {
			queryWrapper.eq("status", status);
		}
		IPage<ProductType> page = new Page<ProductType>(pageNo, pageSize);
		page.setCurrent(pageNo);
		page.setSize(pageSize);
		page = productTypeService.page(page, queryWrapper);
		result.put("total", page.getTotal());
		result.put("pageNumber", pageNo);
		result.put("rows",JSON.toJSON(page.getRecords()));
		logger.info("查询结果======"+result);
		responseJson(response, result);
	}
	/**
	 * 
	 * @author: 姚雁飞
	 * @desc: 添加产品分类
	 * @date: 2020年4月20日 下午5:40:30
	 * @param request
	 * @param response
	 * @param name
	 * @param status
	 * @throws IOException
	 */
	@PostMapping("/addProuctType")
	public void addProuctType(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "name", required = true) String name,
			@RequestParam(value = "status", required = true,defaultValue = "1") Integer status) throws IOException {
		JSONObject result = new JSONObject();
		if (StringUtils.isEmpty(name)) {
			result.put("code", StatausEnum.Error.getCode());
			result.put("msg", "产品分类名不能为空！");
			responseJson(response, result);
			return;
		}
		QueryWrapper<ProductType> queryWrapper = new QueryWrapper<ProductType>();
		queryWrapper.eq("name", name);
		ProductType productType = productTypeService.getOne(queryWrapper);
		if (productType != null) {
			result.put("code", StatausEnum.Error.getCode());
			result.put("msg", "已经存在该分类，请换分类名重新录入！");
			responseJson(response, result);
			return;
		}
		ProductType entity = new ProductType();
		entity.setName(name);
		entity.setStatus(status);
		Date date = new Date();
		entity.setCreateTime(date);
		boolean falg = productTypeService.save(entity);
		if (falg) {
			result.put("code", StatausEnum.SUCCESS.getCode());
			result.put("msg", "添加产品分类成功！");
			responseJson(response, result);
			return;
		} else {
			result.put("code", StatausEnum.Error.getCode());
			result.put("msg", "添加产品分类失败！");
			responseJson(response, result);
			return;
		}
	}
	/**
	 * 
	 * @author: 姚雁飞
	 * @desc: 根据产品分类的id查询分类信息
	 * @date: 2020年4月20日 下午5:01:16
	 * @param request
	 * @param response
	 * @param id 分类的id
	 * @throws IOException
	 */
	@PostMapping("/getProductTypeById")
	public void getProductTypeById(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "id", required = true, defaultValue = "") String id) throws IOException {
		JSONObject result = new JSONObject();
		result.put("code", StatausEnum.SUCCESS.getCode());
		result.put("msg",  StatausEnum.SUCCESS.getMsg());
		if(StringUtils.isEmpty(id)) {
			result.put("code", StatausEnum.Error.getCode());
			result.put("msg",  "id不能为空！");
			responseJson(response, result);
			return;
		}
		ProductType productType = productTypeService.getById(id);
		result.put("data", productType);
		responseJson(response, result);
	}
	/**
	 * 
	 * @author: 姚雁飞
	 * @desc: 根据产品分类id来更新分类信息
	 * @date: 2020年4月20日 下午5:02:50
	 * @param request
	 * @param response
	 * @param id 产品分类id
	 * @param name 产品分类名称
	 * @param status  产品分类状态
	 * @throws IOException
	 */
	@PostMapping("/updateProductTypeById")
	public void updateProductTypeById(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "id", required = true, defaultValue = "") String id,
			@RequestParam(value = "name", required = false, defaultValue = "") String name,
			@RequestParam(value = "status", required = false, defaultValue = "") String status) throws IOException {
		JSONObject result = new JSONObject();
		result.put("code", StatausEnum.SUCCESS.getCode());
		result.put("msg", StatausEnum.SUCCESS.getMsg());
		if (StringUtils.isEmpty(id)) {
			result.put("code", StatausEnum.Error.getCode());
			result.put("msg", "id不能为空！");
			responseJson(response, result);
			return;
		}
		ProductType productType = productTypeService.getById(id);
		if (StringUtils.isNotEmpty(name)) {
			productType.setName(name);
		}
		if (StringUtils.isNotEmpty(status)) {
			productType.setStatus(Integer.valueOf(status));
		}
		productTypeService.saveOrUpdate(productType);
		responseJson(response, result);
	}
	
	
}
